package {{{ .PackageName }}}

import (
	"gorm.io/gorm"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/util"
)

type I{{{ title (toCamelCase .EntityName) }}}Service interface {
	List(c *gin.Context, page request.PageReq, listReq req.{{{ title (toCamelCase .EntityName) }}}ListReq) (res response.PageResp, e error)
	Detail(c *gin.Context, id uint) (res resp.{{{ title (toCamelCase .EntityName) }}}Resp, e error)
	Add(c *gin.Context, addReq req.{{{ title (toCamelCase .EntityName) }}}AddReq) (e error)
	Edit(c *gin.Context, editReq req.{{{ title (toCamelCase .EntityName) }}}EditReq) (e error)
	Del(c *gin.Context, id uint) (e error)
}

//New{{{ title (toCamelCase .EntityName) }}}Service 初始化
func New{{{ title (toCamelCase .EntityName) }}}Service(db *gorm.DB,dao *dao.Query) I{{{ title (toCamelCase .EntityName) }}}Service {
	return &{{{ toCamelCase .EntityName }}}Service{db: db,dao: dao}
}

//{{{ toCamelCase .EntityName }}}Service {{{ .FunctionName }}}服务实现类
type {{{ toCamelCase .EntityName }}}Service struct {
	db *gorm.DB
	dao *dao.Query
}

//List {{{ .FunctionName }}}列表
func (srv {{{ toCamelCase .EntityName }}}Service) List(c *gin.Context, page request.PageReq, listReq req.{{{ title (toCamelCase .EntityName) }}}ListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)

    m := srv.dao.{{{ title (toCamelCase .EntityName) }}}
    q := m.WithContext(c)

	// 查询
	{{{- range .Columns }}}
	{{{- if .IsQuery }}}
	{{{- $queryOpr := index $.ModelOprMap .QueryType }}}
	{{{- if and (eq .GoType "string") (eq $queryOpr "like") }}}
	if listReq.{{{ title (toCamelCase .ColumnName) }}} != "" {
        q = q.Where(m.{{{(toCamelCase .ColumnName) }}}.Like("%"+listReq.{{{ title (toCamelCase .ColumnName) }}}+"%"))
    }
    {{{- else }}}
    if listReq.{{{ title (toCamelCase .ColumnName) }}} {{{ if eq .GoType "string" }}}!= ""{{{ else }}}>=0{{{ end }}} {
        q = q.Where(m.{{{ title (toCamelCase .ColumnName) }}}.Eq(listReq.{{{ title (toCamelCase .ColumnName) }}}))
    }
    {{{- end }}}
    {{{- end }}}
    {{{- end }}}
	{{{- if contains .AllFields "is_delete" }}}
	q = q.Where(m.IsDelete.Eq(0))
	{{{- end }}}

	// 总数
	count, err := q.Count()
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	objs, err := q.Limit(limit).Offset(offset).Order(m.ID.Desc()).Find()
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}
	var resps  []resp.{{{ title (toCamelCase .EntityName) }}}Resp
	response.Copy(&resps, objs)
	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

//Detail {{{ .FunctionName }}}详情
func (srv {{{ toCamelCase .EntityName }}}Service) Detail(c *gin.Context, id uint) (res resp.{{{ title (toCamelCase .EntityName) }}}Resp, e error) {
    m := srv.dao.{{{ title (toCamelCase .EntityName) }}}
    q := m.WithContext(c)

	obj, err := q.Where(m.{{{ title (toCamelCase $.PrimaryKey) }}}.Eq(id) {{{ if contains .AllFields "is_delete" }}} , m.IsDelete.Eq(0) {{{ end }}}).First()
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}
	response.Copy(&res, obj)
	{{{- range .Columns }}}
    {{{- if and .IsEdit (contains (slice "image" "avatar" "logo" "img") .GoField) }}}
    res.Avatar = util.UrlUtil.ToAbsoluteUrl(res.Avatar)
    {{{- end }}}
    {{{- end }}}
	return
}

//Add {{{ .FunctionName }}}新增
func (srv {{{ toCamelCase .EntityName }}}Service) Add(c *gin.Context, addReq req.{{{ title (toCamelCase .EntityName) }}}AddReq) (e error) {
    m := srv.dao.{{{ title (toCamelCase .EntityName) }}}
    q := m.WithContext(c)

	var obj {{{ title (toCamelCase .EntityName) }}}
	response.Copy(&obj, addReq)
	err := q.Create(&obj)
	e = response.CheckErr(err, "Add Create err")
	return
}

//Edit {{{ .FunctionName }}}编辑
func (srv {{{ toCamelCase .EntityName }}}Service) Edit(c *gin.Context, editReq req.{{{ title (toCamelCase .EntityName) }}}EditReq) (e error) {
    m := srv.dao.{{{ title (toCamelCase .EntityName) }}}
    q := m.WithContext(c)

	var obj {{{ title (toCamelCase .EntityName) }}}
	obj, err := q.Where(m.{{{ title (toCamelCase $.PrimaryKey) }}}.Eq(id) {{{ if contains .AllFields "is_delete" }}} , m.IsDelete.Eq(0) {{{ end }}}).First()
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Edit First err"); e != nil {
		return
	}
	// 更新
	response.Copy(&obj, editReq)
	err = q.Updates(obj)
	e = response.CheckErr(err, "Edit Updates err")
	return
}

//Del {{{ .FunctionName }}}删除
func (srv {{{ toCamelCase .EntityName }}}Service) Del(c *gin.Context, id uint) (e error) {
    m := srv.dao.{{{ title (toCamelCase .EntityName) }}}
    q := m.WithContext(c)

	_, err := q.Where(m.{{{ title (toCamelCase $.PrimaryKey) }}}.Eq(id) {{{ if contains .AllFields "is_delete" }}} , m.IsDelete.Eq(0) {{{ end }}}).First()
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Del First err"); e != nil {
		return
	}
    // 删除
    {{{- if contains .AllFields "is_delete" }}}
    err = q.Where(m.{{{ title (toCamelCase $.PrimaryKey) }}}.Eq(id)).Update(m.IsDelete, 1)
    e = response.CheckErr(err, "Del Save err")
    {{{- else }}}
    err = q.Where(m.{{{ title (toCamelCase $.PrimaryKey) }}}.Eq(id)).Delete()
    e = response.CheckErr(err, "Del Delete err")
    {{{- end }}}
	return
}
